package com.hundsun.yr.universal.p2p.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.p2p.bean.NewsSlideShowBean;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.ui.activity.WebNewsShareActivity;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.widget.JDAdverView;

import java.util.List;


/**
 * Created by Administrator on 2016/3/20.
 * 京东广告栏数据适配器
 *
 */

public class JDViewAdapter {
    private List<NewsSlideShowBean> mDatas;
    private Context mContext;

    public JDViewAdapter(List<NewsSlideShowBean> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        if (mDatas == null || mDatas.isEmpty()) {
            throw new RuntimeException("nothing to show");
        }
    }

    /**
     * 获取数据的条数
     * @return
     */
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    /**
     * 获取摸个数据
     * @param position
     * @return
     */
    public NewsSlideShowBean getItem(int position) {
        return mDatas.get(position);
    }

    /**
     * 获取条目布局
     * @param parent
     * @return
     */
    public View getView(JDAdverView parent) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, null);
    }

    /**
     * 条目数据适配
     * @param view
     * @param data
     */
    public void setItem(final View view, final NewsSlideShowBean data) {

        TextView tv = (TextView) view.findViewById(R.id.title);
        tv.setText(data.title);

        TextView tag = (TextView) view.findViewById(R.id.tag);
        tag.setText(CommTool.formartDate2(Long.valueOf(data.ctime) * 1000));

        //你可以增加点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //比如打开url
                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, mContext.getString(R.string.root_h5_url_test) + mContext.getString(R.string.h5_index) + TaskConstants.TASK_NEWSLIST);
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, mContext.getString(R.string.more_news_detail));
                Intent intent = new Intent(mContext, WebNewsShareActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }


}
